package com.wbao.dianniu.utils;

import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AddorDelCollectManager;

/**
 * 评论长点击处理类
 * <p>
 * Created by yejianjun on 2017/10/10.
 */

public class CommentLongClickHelper {


    private static CommentLongClickHelper instance;
    public ICommentLongListener mListener = null;

    public static CommentLongClickHelper getInstance() {
        if (null == instance) {
            instance = new CommentLongClickHelper();
        }
        return instance;
    }

    public interface ICommentLongListener {
        public void commentLongClick(QuestAnswerListData data);
    }

    public void addCommentLongClickListener(ICommentLongListener listener) {
        mListener = listener;
    }

    public void removeCommentLongClickListener(ICommentLongListener listener) {
        mListener = null;
    }

    public void commentLongClick(final Context context, final QuestAnswerListData data, final int type, final String replyName) {
        final Dialog dialog = new Dialog(context, R.style.my_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.custom_bottom_dialog, null);
        Button replyBtn = (Button) view.findViewById(R.id.custom_bottom_reply);
        View replyLine = (View) view.findViewById(R.id.custom_bottom_reply_line);
        Button copyBtn = (Button) view.findViewById(R.id.custom_bottom_copy);
        View copyLine = (View) view.findViewById(R.id.custom_bottom_copy_line);
        Button reportBtn = (Button) view.findViewById(R.id.custom_bottom_report);
        View reportLine = (View) view.findViewById(R.id.custom_bottom_report_line);
        Button deleteBtn = (Button) view.findViewById(R.id.custom_bottom_delete);
        Button cancelBtn = (Button) view.findViewById(R.id.custom_bottom_cancel);

        // 自己的评论 显示删除和复制
        if (data.userInfo.accountId == GlobalContext.getAccountId()) {
            replyBtn.setVisibility(View.GONE);
            replyLine.setVisibility(View.GONE);
            copyBtn.setVisibility(View.VISIBLE);
            copyLine.setVisibility(View.VISIBLE);
            reportBtn.setVisibility(View.GONE);
            reportLine.setVisibility(View.GONE);
            deleteBtn.setVisibility(View.VISIBLE);
        } else { // 别人的评论，显示 回复，复制，举报 和取消
            replyBtn.setVisibility(View.VISIBLE);
            replyLine.setVisibility(View.VISIBLE);
            copyBtn.setVisibility(View.VISIBLE);
            copyLine.setVisibility(View.VISIBLE);
            reportBtn.setVisibility(View.VISIBLE);
            reportLine.setVisibility(View.VISIBLE);
            deleteBtn.setVisibility(View.GONE);
        }

        Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = view.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        win.setAttributes(lp);

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        // 回复
        replyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                ReplyHelper.getInstance().reply(replyName, data.accountId);
            }
        });
        // copy
        copyBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(data.content);
                Notification.showToastMsg(context,"内容已复制");
            }
        });
        // 举报
        reportBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                Utils.intoReportActivity(context, type, data.id);
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (null != mListener) {
                    mListener.commentLongClick(data);
                }
            }
        });
        dialog.setContentView(view);
        dialog.show();;
    }

    /**
     * 显示复制和取消
     */
    public Dialog showCopyDialog(final Context context, final String content) {
        final Dialog dialog = new Dialog(context, R.style.my_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.custom_bottom_dialog, null);
        Button replyBtn = (Button) view.findViewById(R.id.custom_bottom_reply);
        View replyLine = (View) view.findViewById(R.id.custom_bottom_reply_line);
        Button copyBtn = (Button) view.findViewById(R.id.custom_bottom_copy);
        View copyLine = (View) view.findViewById(R.id.custom_bottom_copy_line);
        Button reportBtn = (Button) view.findViewById(R.id.custom_bottom_report);
        View reportLine = (View) view.findViewById(R.id.custom_bottom_report_line);
        Button deleteBtn = (Button) view.findViewById(R.id.custom_bottom_delete);
        Button cancelBtn = (Button) view.findViewById(R.id.custom_bottom_cancel);

        replyBtn.setVisibility(View.GONE);
        replyLine.setVisibility(View.GONE);
        copyBtn.setVisibility(View.VISIBLE);
        copyLine.setVisibility(View.VISIBLE);
        reportBtn.setVisibility(View.GONE);
        reportLine.setVisibility(View.GONE);
        deleteBtn.setVisibility(View.GONE);

        Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = view.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        win.setAttributes(lp);

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        // 复制
        copyBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(content);
                Notification.showToastMsg(context,"内容已复制");
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.show();

        return dialog;
    }

    /**
     * 显示举报弹出框
     *
     * @param context
     * @param type
     * @param id
     */
    public void showReportDialog(final Context context, final int type, final int id) {
        final Dialog dialog = new Dialog(context, R.style.my_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.custom_bottom_dialog, null);
        Button replyBtn = (Button) view.findViewById(R.id.custom_bottom_reply);
        View replyLine = (View) view.findViewById(R.id.custom_bottom_reply_line);
        Button copyBtn = (Button) view.findViewById(R.id.custom_bottom_copy);
        View copyLine = (View) view.findViewById(R.id.custom_bottom_copy_line);
        Button reportBtn = (Button) view.findViewById(R.id.custom_bottom_report);
        View reportLine = (View) view.findViewById(R.id.custom_bottom_report_line);
        Button deleteBtn = (Button) view.findViewById(R.id.custom_bottom_delete);
        Button cancelBtn = (Button) view.findViewById(R.id.custom_bottom_cancel);

        replyBtn.setVisibility(View.GONE);
        replyLine.setVisibility(View.GONE);
        copyBtn.setVisibility(View.GONE);
        copyLine.setVisibility(View.GONE);
        reportBtn.setVisibility(View.VISIBLE);
        reportLine.setVisibility(View.VISIBLE);
        deleteBtn.setVisibility(View.GONE);

        Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = view.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        win.setAttributes(lp);

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        reportBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                Utils.intoReportActivity(context, type, id);
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.show();

    }


    /**
     * 显示收藏 举报弹出框
     */
    public void showCollectReportDialog(final Context context, final AddorDelCollectManager collectManager, int isCollect, final int questId) {
        final Dialog dialog = new Dialog(context, R.style.my_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.custom_bottom_dialog, null);
        Button replyBtn = (Button) view.findViewById(R.id.custom_bottom_reply);
        View replyLine = (View) view.findViewById(R.id.custom_bottom_reply_line);
        Button copyBtn = (Button) view.findViewById(R.id.custom_bottom_copy);
        View copyLine = (View) view.findViewById(R.id.custom_bottom_copy_line);
        Button reportBtn = (Button) view.findViewById(R.id.custom_bottom_report);
        View reportLine = (View) view.findViewById(R.id.custom_bottom_report_line);
        Button deleteBtn = (Button) view.findViewById(R.id.custom_bottom_delete);
        Button cancelBtn = (Button) view.findViewById(R.id.custom_bottom_cancel);

        replyBtn.setVisibility(View.GONE);
        replyLine.setVisibility(View.GONE);
        copyBtn.setVisibility(View.VISIBLE);
        copyLine.setVisibility(View.VISIBLE);
        reportBtn.setVisibility(View.VISIBLE);
        reportLine.setVisibility(View.VISIBLE);
        deleteBtn.setVisibility(View.GONE);

        if (1 == isCollect) {
            copyBtn.setText("取消收藏");
        } else {
            copyBtn.setText("收藏");
        }

        Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = (int) context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = view.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        win.setAttributes(lp);

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        // 收藏
        copyBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                collectManager.addorDelCollect(GlobalContext.getAccountId(), questId);
            }
        });
        reportBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                Utils.intoReportActivity(context, ReportType.TYPE_QA, questId);
            }
        });
        // 取消
        cancelBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.setContentView(view);
        dialog.show();

    }


}
